/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package FamousBallet;

import java.util.regex.Pattern;

/**
 *<pre>
 File name  Validation.java
 * @author    <b><i>Yerong</i></b>
 * Instructor  Niko Culevski
 * Class       CS 141 B
 * Due date    11/29/2016
 * Assignment  Project_3
 * @version   1.0.1
 * @see       java.util.regex.Pattern
 * Platform    PC, Windows 10, NetBeans 8.1
 *******************************************************************************
* Validation class uses regular expressions to validate the user input. Those 
* validations include the validation of ballet name, composer name, city name, 
* link, year, and synopsis. 
* 
* All the variables and methods are static, so that we do not have to create an
* object to use all the methods.
 *******************************************************************************
 * Input:    ballet name, composer name, city name, link, synopsis, year
 * Output:   Whether the input is valid
 *******************************************************************************
 * </pre>
 */
public class Validation {
    //A ballet name contains one or more words begin with an upper case letter.
    final static Pattern BALLET_NAME = Pattern.compile("[A-Z][a-z']*(\\s[A-Za-z'"
            +"]+)*");

    //A name contains two or three words begin with an upper case letter.
    final static Pattern NAME = Pattern.compile("[A-Z][a-z]+\\s[A-Z][a-z]*(\\s"
            +"[A-Z][a-z]+)?");
    
    /*A link should begin with http or https and \\ and any letters regardless
    of case and a dot and any letters regardless of case and / and any letters 
    or numbers repeating more than once.*/
    final static Pattern LINK = Pattern.compile("(((http)|(https)):\\/\\/[A-Za"
            +"-z]+(.[A-Za-z]+){1,2}\\/[A-Za-z0-9?\\-=]+)");
    
    // A city contains one word begin with an upper case letter.
    final static Pattern CITY = Pattern.compile("[A-Z][A-Za-z.]+");
    
    //Year contains 4 numbers
    final static Pattern YEAR = Pattern.compile("(1|2)[0-9]{3}");
    
    //Sentences begin with an upper letter and contain several "space and words"
    final static Pattern SENTENCES = Pattern.compile("[A-Z][a-z0-9]+(\\,|\\.|"+
            "\\'|\\s|[A-Za-z0-9\\-\\&\\!]+)*");
       
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method isValidBalletName()
     * Check whether the input matches the pattern.
     * @param text,the Ballet name
     * @return whether the text is valid.
     * @author Yerong
     * Date: 11/29/2016
     */       
    public static boolean isValidBalletName(String text){
    if(!BALLET_NAME.matcher(text).matches()){
        return false;}
        else return true;            
    }
    
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method isValidName()
     * Check whether the input matches the pattern.
     * @param text,the composer name
     * @return whether the text is valid.
     * @author Yerong
     * Date: 11/29/2016
     */       
    public static boolean isValidName(String text){
        //If it doesn't match the pattern, show the message box
        if(!NAME.matcher(text).matches()){
            return false;}
            else return true;            
    }
    
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method isValidLink()
     * Check whether the link matches the pattern.
     * @param text,the link
     * @return whether the text is valid.
     * @author Yerong
     * Date: 11/29/2016
     */         
    public static boolean isValidLink(String text){
        //If it doesn't match the pattern, show the message box
        if(!LINK.matcher(text).matches()){
            return false;}
        else return true;
    }
    
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method isValidCity()
     * Check whether the city name matches the pattern.
     * @param text,the city name
     * @return whether the text is valid.
     * @author Yerong
     * Date: 11/29/2016
     */    
    public static boolean isValidCity(String text){
        //If it doesn't match the pattern, show the message box
        if(!CITY.matcher(text).matches()){
            return false;}
        else return true;
    }
    
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method isValidYear()
     * Check whether the year matches the pattern.
     * @param text,the year
     * @return whether the text is valid.
     * @author Yerong
     * Date: 11/29/2016
     */     
    public static boolean isValidYear(String text){
        //If it doesn't match the pattern, show the message box
        if(!YEAR.matcher(text).matches()){
            return false;}
        else return true;
    }
    
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method isValidSentences()
     * Check whether the synopsis matches the pattern.
     * @param text,the synopsis
     * @return whether the text is valid.
     * @author Yerong
     * Date: 11/29/2016
     */         
    public static boolean isValidSentences(String text){
        //If it doesn't match the pattern, show the message box
        if(!SENTENCES.matcher(text).matches()){
            return false;}
        else return true;
    }

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method isValidBallet()
     * Check whether the ballet object is valid.
     * @param obj,the ballet object
     * @return whether the ballet object is valid.
     * @author Yerong
     * Date: 11/29/2016
     */      
    public static boolean isValidBallet(Ballet obj){
        if(!(Validation.isValidBalletName(obj.getName())&&
            Validation.isValidName(obj.getComposer())&&
            Validation.isValidCity(obj.getCity())&&
            Validation.isValidLink(obj.getLink())&&
            Validation.isValidYear(String.valueOf(obj.getYear()))&&
            Validation.isValidSentences(obj.getSynopsis())))
            return false;
        else return true;
    }
}